package com.yugao.fintech.antelope.infra.file.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.antelope.infra.file.service.FileRecordService;
import com.yugao.fintech.antelope.infra.module.file.model.rqrs.FileRecordPageReq;
import com.yugao.fintech.antelope.infra.module.file.model.rqrs.FileRecordResp;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.framework.core.response.Resp;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 文件上传记录
 */
@Api(tags = "文件上传记录")
@RestController
@RequestMapping("/fileRecord")
public class FileRecordController {
    @Autowired
    private FileRecordService fileRecordService;

    /**
     * 分页 文件上传记录
     */
    @RamResource(action = "file:fileRecord:query", name = "查询文件上传记录", type = ActionType.READ)
    @GetMapping("/pageFileRecord")
    public Resp<IPage<FileRecordResp>> pageFileRecord(@Validated FileRecordPageReq req) {
        return Resp.ok(fileRecordService.pageFileRecord(req));
    }

    /**
     * 通过id查找 文件上传记录
     */
    @RamResource(action = "file:fileRecord:query", name = "查询文件上传记录", type = ActionType.READ)
    @GetMapping("/getFileRecordById")
    public Resp<FileRecordResp> getFileRecord(@RequestParam(value = "id") Long id) {
        return Resp.ok(fileRecordService.getRecordById(id));
    }

    /**
     * 通过文件上传路径查询查找 文件上传记录
     */
    @RamResource(action = "file:fileRecord:query", name = "查询文件上传记录", type = ActionType.READ)
    @GetMapping("/getFileRecordByPath")
    public Resp<FileRecordResp> getFileRecordByPath(@RequestParam(value = "path") String path,
                                                    @RequestParam(value = "isIntranet", defaultValue = "false") Boolean isIntranet) {
        return Resp.ok(fileRecordService.getRecordByPath(path, isIntranet));
    }

    /**
     * 批量删除 文件上传记录
     */
    @RamResource(action = "file:fileRecord:delete", name = "删除文件上传记录", type = ActionType.WRITE)
    @DeleteMapping("/deleteFileRecordByIds")
    public Resp<?> deleteBatch(@RequestParam(value = "ids") List<Long> ids) throws Exception {
        return Resp.ok(fileRecordService.deleteRecordByIds(ids));
    }

    @RamResource(action = "file:fileRecord:delete", name = "删除文件上传记录", type = ActionType.WRITE)
    @DeleteMapping(value = "/deleteFileRecordByPath")
    public Resp<?> deleteRecordByPath(@RequestParam(value = "path") String path) {
        fileRecordService.deleteRecordByPath(path);
        return Resp.ok();
    }

    /**
     * 删除 文件上传记录
     */
    @RamResource(action = "file:fileRecord:delete", name = "删除文件上传记录", type = ActionType.WRITE)
    @DeleteMapping("/deleteFileRecordById")
    public Resp<?> delete(@RequestParam(value = "id") Long id) throws Exception {
        return Resp.ok(fileRecordService.deleteRecordById(id));
    }
}
